<?php
/**
 *   Joomdiet - administrator/components/controllers/filereceiver.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');

class JoomdietControllerFileReceiver extends JController
{
    function __construct() {
        // get requested view
        if(JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        parent::__construct();
        
        $this->registerTask('addFood', 'editFood');
    }
    
    function processfile()
    {
        $csvfile = '/var/www/joomla/base.csv';
    
        if (!file_exists($csvfile)) {
            echo "File not found. Make sure you specified the correct path.\n";
            exit;
        }
        
        $file = fopen($csvfile, "r");
        
        if (!$file) {
            echo "Error opening file.\n";
            exit;
        }
        
        $size = filesize($csvfile);
        
        if (!$size) {
            echo "File is empty.\n";
            exit;
        }
        
        $csvcontent = fread($file, $size);
        fclose($file);
        
        $lines = 0;
        $linearray = array();
        $result = array();
        
        foreach(split("\n", $csvcontent) as $line) {
            $lines++;
            $line = trim($line, " \t");
            $line = str_replace("\r", "", $line);
            $line = str_replace("'",  "\'", $line);
            $linearray = explode(";", $line);
            $result[] = $linearray;
        }
        
        // loading models
        $variable_model = $this->getModel('Variable');
        $food_model = $this->getModel('Food');
        $food_variable_model = $this->getModel('FoodVariable');
        
        foreach($result[0] as $k => $header) {
            if ($k > 1) {
                $variable = $variable_model->getVariableByName($header);
                if (isset($variable) or ($variable != 0)) {
                    $result[0][$k] = $variable->id;
                } else $result[0][$k] = -1;
            }
        }
        
        for ($i = 1; $i < count($result); $i++) {
            if (is_array($result[$i]) && (count($result[$i]) > 2)) {
                $foodname = $result[$i][0];
                $foodcategory = $result[$i][1]+1;
                $food_id = $food_model->insert($foodname, $foodcategory);
                for ($j = 2; $j < count($result[0]); $j++) {
                    $variable_id = $result[0][$j];
                    if ($variable_id != -1) {
                        $food_variable_model->insert($food_id, $variable_id, $result[$i][$j]);
                    }
                }
            }
        }
    }
    
}
?>
